package com.taotao.tools.validate.validator;

import com.taotao.tools.core.reflect.ReflectUtils;
import com.taotao.tools.validate.annotations.UnionConstraint;
import com.taotao.tools.validate.annotations.UnionValids;
import com.taotao.tools.validate.annotations.union.EqualsTo;
import lombok.extern.slf4j.Slf4j;

import javax.validation.ConstraintValidator;
import javax.validation.ConstraintValidatorContext;
import java.lang.reflect.Field;


@Slf4j
public class UnionFieldValidator implements ConstraintValidator<UnionValids, Object> {

    private UnionValids unionValids;

    @Override
    public void initialize(UnionValids unionValid) {
        this.unionValids = unionValid;

    }

    @Override
    public boolean isValid(Object targetObj, ConstraintValidatorContext context) {
        Field[] fields = targetObj.getClass().getDeclaredFields();
        for (Field e : fields) {
            Object fv = null;
            try {
                 fv = ReflectUtils.getFieldValue(targetObj, e);
            } catch (IllegalAccessException ex) {
            }
            if(null == fv){
                continue;
            }

           if(!doUnionFieldValidate(targetObj,e,fv,context)){
               return false;
           }

        }


        return true;
    }

    private boolean doUnionFieldValidate(Object targetObj,Field f, Object fv, ConstraintValidatorContext context) {
        EqualsTo anno = f.getAnnotation(EqualsTo.class);
        UnionConstraint uc = anno.annotationType().getAnnotation(UnionConstraint.class);


        return true;


    }


}
